<template>
  <div>
    <!--面包屑导航区域-->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>宠物管理</el-breadcrumb-item>
    </el-breadcrumb>

    <!--卡片视图区域-->
    <el-card>
      <el-row :gutter="20">
        <el-col :span="7">
          <el-input
            placeholder="请输入内容"
            v-model="queryInfo.searchName"
            clearable
            @clear="getUserList"
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="getUserList"
            ></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="addDialogVisible = true"
            >添加宠物</el-button
          >
        </el-col>
      </el-row>

      <!--会员列表区域-->
      <el-table :data="userlist" border stripe>
        <el-table-column type="index" label="#"></el-table-column>
        <el-table-column prop="Petid" label="宠物编号"></el-table-column>
        <el-table-column prop="Petname" label="宠物昵称"></el-table-column>
        <el-table-column prop="Petvar" label="宠物品种"></el-table-column>
        <el-table-column prop="Petage" label="宠物年龄"></el-table-column>
        <el-table-column prop="Petmemid" label="主人编号"></el-table-column>
        <el-table-column prop="Petremark" label="备注"></el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="small"
              @click="
                showEditDialog(
                  scope.row.Petid,
                  scope.row.Petname,
                  scope.row.Petvar,
                  scope.row.Petage,
                  scope.row.Petmemid,
                  scope.row.Petremark
                )
              "
            ></el-button>
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="small"
              @click="removeUserById(scope.row.Petid)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>

      <!--分页区域-->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.page"
        :page-sizes="[3, 5, 8, 10]"
        :page-size="queryInfo.limit"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>

    <!--添加信息的对话框-->
    <el-dialog
      title="添加宠物信息"
      :visible.sync="addDialogVisible"
      width="50%"
      @close="addDialogClosed"
    >
      <!-- 内容主体区 -->
      <el-form
        :model="addForm"
        :rules="addFormRules"
        ref="addFormRef"
        label-width="70px"
      >
        <el-form-item label="编号" prop="petid">
          <el-input v-model="addForm.petid"></el-input>
        </el-form-item>
        <el-form-item label="昵称" prop="petname">
          <el-input v-model="addForm.petname"></el-input>
        </el-form-item>
        <el-form-item label="品种" prop="petvar">
          <el-input v-model="addForm.petvar"></el-input>
        </el-form-item>
        <el-form-item label="年龄" prop="petage">
          <el-input v-model="addForm.petage"></el-input>
        </el-form-item>
        <el-form-item label="会员" prop="petmemid">
          <el-input v-model="addForm.petmemid"></el-input>
        </el-form-item>
        <el-form-item label="备注" prop="petremark">
          <el-input v-model="addForm.petremark"></el-input>
        </el-form-item>
      </el-form>
      <!-- 底部区域 -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="addDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addUser">确 定</el-button>
      </span>
    </el-dialog>

    <!--修改用户的对话框-->
    <el-dialog
      title="修改宠物信息"
      :visible.sync="editDialogVisible"
      width="50%"
      @close="editDialogClosed"
    >
      <!-- 内容主体区 -->
      <el-form
        :model="editForm"
        :rules="editFormRules"
        ref="editFormRef"
        label-width="70px"
      >
        <el-form-item label="编号">
          <el-input v-model="editForm.petid" disabled></el-input>
        </el-form-item>
        <el-form-item label="昵称" prop="petname">
          <el-input v-model="editForm.petname"></el-input>
        </el-form-item>
        <el-form-item label="品种" prop="petvar">
          <el-input v-model="editForm.petvar"></el-input>
        </el-form-item>
        <el-form-item label="年龄" prop="petage">
          <el-input v-model="editForm.petage"></el-input>
        </el-form-item>
        <el-form-item label="会员">
          <el-input v-model="editForm.petmemid" disabled></el-input>
        </el-form-item>
        <el-form-item label="备注" prop="petremark">
          <el-input v-model="editForm.petremark"></el-input>
        </el-form-item>
      </el-form>
      <!-- 底部区域 -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="editDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="editUser()">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>


<script>
export default {
  data() {
    return {
      queryInfo: {
        //搜索关键字
        searchName: "",
        page: 1,
        limit: 10,
      },
      userlist: [],
      total: 0,
      //控制添加用户对话框的显示与隐藏
      addDialogVisible: false,
      //添加用户的表单数据
      addForm: {
        petid: "",
        petname: "",
        petvar: "",
        petage: "",
        petmemid: "",
        petremark: "",
      },
      //添加表单的验证规则对象
      addFormRules: {
        petid: [
          { required: true, message: "请输入宠物编号", trigger: "blur" }
        ],
        petname: [
          { required: true, message: "请输入宠物昵称", trigger: "blur" }
        ],
        petage: [
          { required: true, message: "请输入宠物年龄", trigger: "blur" }
        ],
        petmemid: [
          { required: true, message: "请输入宠物主人编号", trigger: "blur" }
        ]
      },
      //控制修改对话框的显示与隐藏
      editDialogVisible: false,
      editForm: {
        petid: "",
        petname: "",
        petvar: "",
        petage: "",
        petmemid: "",
        petremark: ""
      },
      //修改表单的验证规则对象
      editFormRules: {
        petid: [
          { required: true, message: "请输入宠物编号", trigger: "blur" }
        ],
        petname: [
          { required: true, message: "请输入宠物昵称", trigger: "blur" }
        ],
        petage: [
          { required: true, message: "请输入宠物年龄", trigger: "blur" }
        ],
        petmemid: [
          { required: true, message: "请输入宠物主人编号", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getUserList();
  },
  methods: {
    async getUserList() {
      const { data: res } = await this.$http.get("system/apis/pets", {
        params: this.queryInfo
      });
      console.log(res);
      if (res.code !== 20000) {
        return this.$message.error("获取宠物列表失败！")
      }
      this.userlist = res.data.value;
      this.total = res.data.total;
    },
    //监听pagesize改变的事件
    handleSizeChange(newSize) {
      //console.log(newSize)
      this.queryInfo.limit = newSize
      this.getUserList()
    },
    //监听页码值改变的事件
    handleCurrentChange(newPage) {
      //console.log(newPage)
      this.queryInfo.page = newPage
      this.getUserList()
    },
    //监听添加用户对话框的关闭事件
    addDialogClosed() {
      this.$refs.addFormRef.resetFields();
    },
    //监听修改用户对话框的关闭事件
    editDialogClosed() {
      this.$refs.editFormRef.resetFields();
    },
    //点击按钮添加信息
    addUser() {
      this.$refs.addFormRef.validate(async (valid) => {
        if (!valid) return;
        //可以添加新用户的网络请求
        const { data: res } = await this.$http.post(
          "system/apis/pets",
          this.addForm
        );

        if (res.code == 20000) {
          this.$message.success("添加成功！");
        }else{
          this.$message.error("添加失败！");
        }

        //隐藏添加用户的对话框
        this.addDialogVisible = false;
        //重新获取用户列表数据
        this.getUserList();
      });
    },
    //编辑用户按钮
    editUser() {
      this.$refs.editFormRef.validate(async (valid) => {
        if (!valid) return;
        //发起修改的网络请求
        const { data: res } = await this.$http.put(
          "system/apis/pets",
          this.editForm
        );

        if (res.code != 20000) {
          return this.$message.error("修改失败！");
        }

        this.$message.success("修改成功！");
        //隐藏添加用户的对话框
        this.editDialogVisible = false;
        //重新获取用户列表数据
        this.getUserList();
      });
    },
    //展示编辑用户的对话框
    async showEditDialog(petid, petname, petvar,petage, petmemid,petremark) {
      this.editDialogVisible = true;
      this.editForm.petid = String(petid);
      this.editForm.petname = String(petname);
      this.editForm.petvar = String(petvar);
      this.editForm.petage = String(petage);
      this.editForm.petmemid = String(petmemid);
      this.editForm.petremark = String(petremark);
    },
    //删除
    async removeUserById(Petid) {
      const confirmResult = await this.$confirm(
        "此操作将永久删除该宠物信息, 是否继续?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      ).catch((err) => err);
      //如果用户确认删除，则返回字符串comfirm，反之为cancel（用catch返回）
      //console.log(confirmResult)
      if (confirmResult !== "confirm") {
        return this.$message.info("已取消删除！")
      }

      //console.log('确认了删除')
      const { data: res } = await this.$http.delete(
        "system/apis/pets?petid=" + Petid
      );
      //if (res.code !== 20000) {
       // return this.$message.error("删除失败！")
      //}
      //this.$message.success("删除成功！")
      if (res.code !== 20000) {
        return this.$message.error("删除失败！")
      }
      this.$message.success("删除成功！")
      this.getUserList();
    },
  },
};
</script>

<style lang="less" scoped>
</style>